package com.taxshare.tools.util;

import cn.hutool.core.util.StrUtil;
import com.taxshare.tools.constant.CommonConstant;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
@Slf4j
public class DateUtil {

	// 上期月度
	public static String lastMonthPeriod(String period) {
		YearMonth yearMonth = YearMonth.parse(period, DateTimeFormatter.ofPattern("yyyy-MM"));
		LocalDate localDate = LocalDate.of(yearMonth.getYear(), yearMonth.getMonthValue(), 1);
		localDate = localDate.plus(-1, ChronoUnit.MONTHS);
		return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM"));
	}

	// 同期月度
	public static String sameMonthPeriod(String period) {
		YearMonth yearMonth = YearMonth.parse(period, DateTimeFormatter.ofPattern("yyyy-MM"));
		LocalDate localDate = LocalDate.of(yearMonth.getYear(), yearMonth.getMonthValue(), 1);
		localDate = localDate.plus(-1, ChronoUnit.YEARS);
		return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM"));
	}

	// 上期年度
	public static String lastYearPeriod(String period) {
		YearMonth yearMonth = YearMonth.parse(period, DateTimeFormatter.ofPattern("yyyy-MM"));
		LocalDate localDate = LocalDate.of(yearMonth.getYear(), yearMonth.getMonthValue(), 1);
		localDate = localDate.plus(-1, ChronoUnit.YEARS);
		return localDate.format(DateTimeFormatter.ofPattern("yyyy"));

	}
	
	public static String getDatetoDay(String dateStr) {
        try {
            DateFormat formatM = new SimpleDateFormat("yyyyMMddHHmmss");
            SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
            return shortSdf.format(formatM.parse(dateStr)) ;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
	
	public static String getDateto(String dateStr) {
        try {
            SimpleDateFormat shortSdfd = new SimpleDateFormat("yyyy-MM");
            DateFormat formatM = new SimpleDateFormat("yyyyMMddHHmmss");
            return shortSdfd.format(formatM.parse(dateStr)) ;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

	public static String exchangeAll(String dateStr) {
		try {
			SimpleDateFormat shortSdfd = new SimpleDateFormat("yyyyMMddHHmmss");
			DateFormat formatM = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return shortSdfd.format(formatM.parse(dateStr)) ;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}


	/**
	 * 获取指定年月的第一天
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getFirstDayOfMonth1(int year, int month) {
		Calendar cal = Calendar.getInstance();
		//设置年份
		cal.set(Calendar.YEAR, year);
		//设置月份
		cal.set(Calendar.MONTH, month-1);
		//获取某月最小天数
		int firstDay = cal.getMinimum(Calendar.DATE);
		//设置日历中月份的最小天数
		cal.set(Calendar.DAY_OF_MONTH,firstDay);
		//格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime());
	}


	/**
	 * 获取指定年月的最后一天
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getLastDayOfMonth1(int year, int month) {
		Calendar cal = Calendar.getInstance();
		//设置年份
		cal.set(Calendar.YEAR, year);
		//设置月份
		cal.set(Calendar.MONTH, month-1);
		//获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DATE);
		//设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		//格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取当前时间 yyyy-MM-dd
	 *
	 * @return
	 */
	public static String getCurrentYmd() {
		return DateFormatUtils.format(Calendar.getInstance(), "yyyy-MM-dd");
	}


	/**
	 * 获取当前月份,格式MM
	 */
	public static String currentMonth() {
		return new SimpleDateFormat(CommonConstant.DATE_FORMAT_THREE).format(new Date()).substring(4, 6);
	}

	public static String formatDateToString6(Date date){
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		return sf.format(date);
	}

	public static String getCurrent() {
		return DateFormatUtils.format(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss SSS");
	}
	/**
	 * 日期格式化
	 * @param dateStr
	 * @return
	 */
	public static String getFormatDate(String dateStr) {
		if(StrUtil.hasBlank(dateStr)){
			return null;
		}
		if(dateStr.length()==8){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date date = sdf.parse(dateStr);
				return sdf2.format(date);
			} catch (ParseException e) {
				log.error("日期格式异常：{}", e);
			}
		}else {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				Date date = sdf.parse(dateStr);
				return sdf2.format(date);
			} catch (ParseException e) {
				log.error("日期格式异常：{}", e);
			}
		}

		return dateStr;
	}

	/**
	 * 日期格式化
	 * @param dateStr
	 * @return
	 */
	public static String getFormatDateTime(String dateStr) {
		if(StrUtil.hasBlank(dateStr)){
			return null;
		}
		if(dateStr.length()==8){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date date = sdf.parse(dateStr);
				return sdf2.format(date);
			} catch (ParseException e) {
				log.error("日期格式异常：{}", e);
			}
		}else {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				Date date = sdf.parse(dateStr);
				String dates = sdf2.format(date);
				String dateTime = getYYYYmmdd(dates);
				return dateTime;
			} catch (ParseException e) {
				log.error("日期格式异常：{}", e);
			}
		}

		return dateStr;
	}

	/**
	 * yyyy-MM-dd转成年月日格式
	 * @param dateStr
	 * @return
	 */
	public static String getDateYear(String dateStr) {
		try {
			SimpleDateFormat shortSdfd = new SimpleDateFormat("yyyy-MM-dd");
			DateFormat formatM = new SimpleDateFormat("yyyy年MM月dd日");
			return formatM.format(shortSdfd.parse(dateStr)) ;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * yyyy-MM-dd转成yyyyMMdd
	 * @param dateStr
	 * @return
	 */
	public static String transDate(String dateStr) {
		try {
			SimpleDateFormat shortSdfd = new SimpleDateFormat("yyyy-MM-dd");
			DateFormat formatM = new SimpleDateFormat("yyyyMMdd");
			return formatM.format(shortSdfd.parse(dateStr)) ;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * yyyy-MM-dd转成年月日格式
	 * @param dateStr
	 * @return
	 */
	public static String getYYYYmmdd(String dateStr) {
		try {
			SimpleDateFormat shortSdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			DateFormat formatM = new SimpleDateFormat("yyyy-MM-dd");
			return formatM.format(shortSdfd.parse(dateStr)) ;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
}
